package user;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import common.interfaces.AbstractHasID;

/**
 * A session associated with a user's login
 * 
 * @author Gal Lalouche
 * 
 */
public class Session extends AbstractHasID<Long> {
	private final User					user;
	private final long					id;
	private final Map<String, Object>	map	= new ConcurrentHashMap<>();
	
	/**
	 * @param user The user this session will be associated with
	 * @param id The session's unique id
	 */
	public Session(User user, long id) {
		super();
		if (user == null) {
			throw new IllegalArgumentException("user is null");
		}
		this.user = user;
		this.id = id;
	}
	
	/**
	 * @return The user
	 */
	public User getUser() {
		return user;
	}
	
	@Override
	public Long getId() {
		return id;
	}
	
	/**
	 * Adds a new object to the session's cache
	 * 
	 * @param name The object's name
	 * @param value The object's value
	 */
	public void put(String name, Object value) {
		if (name == null) {
			throw new IllegalArgumentException("name is null");
		}
		map.put(name, value);
	}
	
	/**
	 * Retrieves a previously stored object
	 * 
	 * @param name The object's name
	 * @return The previously stored object, or <code>null</code> if none were stored, or if the value stored is
	 *         <code>null</code>
	 */
	public Object get(String name) {
		return map.get(name);
	}
	
}
